package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsManagerInfoMapper;
import cc.rengu.igas.bsps.common.entity.ManagerInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/6
 * Updated by zhangxuran on 2020/04/05
 */
public class BspsManagerInfoMapperImpl implements BspsManagerInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertBspsManagerInfo(ManagerInfo managerInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBspsManagerInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, managerInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记拓展经理信息失败,instId:<{}>,signInstId:<{}>,managerId:<{}>,managerName:<{}>,iReturnCode:<{}>",
                    managerInfo.getInstId(), managerInfo.getSignInstId(), managerInfo.getManagerId(), managerInfo.getManagerName(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateBspsManagerInfoById(ManagerInfo managerInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsManagerInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, managerInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记拓展经理信息失败,instId:<{}>,signInstId:<{}>,managerId:<{}>,managerName:<{}>,iReturnCode:<{}>",
                    managerInfo.getInstId(), managerInfo.getSignInstId(), managerInfo.getManagerId(), managerInfo.getManagerName(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ManagerInfo selectBspsManagerInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerInfoById");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, managerInfo, managerInfo);
        if (0 != iReturnCode) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return managerInfo;
    }

    @Override
    public ManagerInfo selectBspsManagerInfoByPrimaryKey(String instId, String signInstId, String managerId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerInfoByPrimaryKey");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setSignInstId(signInstId);
        managerInfo.setManagerId(managerId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, managerInfo, managerInfo);
        if (0 != iReturnCode) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,signInstId:<{}>,managerId:<{}>,iReturnCode:<{}>", instId, signInstId, managerId, iReturnCode);
            return null;
        }
        return managerInfo;
    }

    @Override
    public ManagerInfo selectBspsManagerInfoByOpenId(String instId, String openId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);


        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerInfoByOpenId");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setOpenId(openId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,openId:<{}>", instId, openId);
            return null;
        }
        List<ManagerInfo> managerList = objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
        if (managerList.size() > 1) {
            rglog.error("根据机构号<{}>, openId:<{}>查询用户信息失败！", instId, openId);
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        managerInfo = managerList.get(0);
        return managerInfo;
    }

    @Override
    public List<ManagerInfo> selectBspsManagerInfoBySignInstId(String instId, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerInfoBySignInstId");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setSignInstId(signInstId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,signInstId:<{}>", instId, signInstId);
            return null;
        }
        return objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<ManagerInfo> selectBspsManagerInfoByAdminArea(String instId, String province, String city, String area) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerInfoByAdminArea");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setProvince(province);
        managerInfo.setCity(city);
        managerInfo.setArea(area);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,province:<{}>,city:<{}>,area:<{}>", instId, province, city, area);
            return null;
        }
        return objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
    }

    @Override
    public ManagerInfo selectBspsManagerInfoByNameAndPhone(String instId, String managerName, String managerPhone) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerInfoByNameAndPhone");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setManagerName(managerName);
        managerInfo.setManagerPhone(managerPhone);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,managerName:<{}>,managerPhone:<{}>,area:<{}>", instId, managerName, managerPhone);
            return null;
        }
        List<ManagerInfo> managerInfoList = objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
        if (managerInfoList.size() > 1) {
            rglog.error("姓名、手机号信息相同的拓展经理存在多个", instId, managerName, managerPhone);
            throw new BizException(RespCodeEnum.GET_MANAGER_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MANAGER_INFO_ERROR.getRespDesc());
        }
        managerInfo = managerInfoList.get(0);
        return managerInfo;
    }

    @Override
    public ManagerInfo selectBspsManagerInfoByManagerId(String instId, String managerId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerInfoByManagerId");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setManagerId(managerId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,managerId:<{}>", instId, managerId);
            return null;
        }
        List<ManagerInfo> managerInfoList = objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
        managerInfo = managerInfoList.get(0);
        return managerInfo;
    }

    @Override
    public ManagerInfo selectBspsManagerNameByManagerId(String instId, String managerId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerNameByManagerId");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setManagerId(managerId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,拓展经理工号managerId:<{}>", instId, managerId);
            return null;
        }
        if (objectList.size() > 1) {
            rglog.error("同一拓展经理工号对应多个拓展经理,instId:<{}>,拓展经理工号managerId:<{}>", instId, managerId);
            return null;
        }
        List<ManagerInfo> managerInfoList = objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
        managerInfo = managerInfoList.get(0);
        return managerInfo;
    }

    @Override
    public ManagerInfo selectBspsManagerInfoByManagerPhone(String instId, String managerPhone) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerInfoByManagerPhone");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setManagerPhone(managerPhone);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,拓展经理手机号managerPhone:<{}>", instId, managerPhone);
            return null;
        }
        if (objectList.size() > 1) {
            rglog.error("同一拓展经理工号对应多个拓展经理,instId:<{}>,拓展经理手机号managerPhone:<{}>", instId, managerPhone);
            return null;
        }
        List<ManagerInfo> managerInfoList = objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
        managerInfo = managerInfoList.get(0);
        return managerInfo;
    }

    @Override
    public PageResult selectBspsManagerIdByAgentNoPage(String instId, String agentNo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsManagerIdByAgentNo");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setSignInstId(agentNo);
        return dbsUtil.dbsFetchPageListData(sqlStr, managerInfo, ManagerInfo.class, pageInfo);
    }
}
